<?php

class Default_ProcessFacultyController extends ZF_DefaultController {

    public function indexAction() {
        $model_hot_hospitals = new Default_Model_HospitalDetail();
        $model_faculty = new Default_Model_Faculty();

        $this->view->header = "Search";
        $this->view->hospitals = $model_hot_hospitals->listAll();
        $this->view->faculty = $model_faculty->listAll();


        if ($this->getRequest()->isPost()) {
            $_hospitalName = $this->getRequest()->getParam('hospitalName');
            $_facultyName = $this->getRequest()->getParam('facultyName');
            $_rate = $this->getRequest()->getParam('rate');
            $this->view->curHospital = $_hospitalName;
            $this->view->curFaculty = $_facultyName;
            $this->view->curRate = $_rate;
            $processFaculty = $model_faculty->getByIdHospitalFacultyRate($_hospitalName, $_facultyName, $_rate);
            $this->view->processFaculty = $processFaculty;
        }
    }

    public function detailAction() {
        $_id = $this->getRequest()->getParam('id');
        $model_faculty = new Default_Model_Faculty();
        $data_query = $model_faculty->getById($_id);
        $faculty = array(
            "name" => $data_query[0]['faculty_name'],
            "location" => $data_query[0]['faculty_location'],
            "images" => $data_query[0]['faculty_images'],
            "sumary" => $data_query[0]['faculty_sumary'],
            "desc" => $data_query[0]['faculty_description']
        );
        $hospital = array(
            "name" => $data_query[0]['hospital_name'],
            "id" => $data_query[0]['hospital_id'],
            "address" => $data_query[0]['hospital_address']
        );
        $doctor = array();
        foreach ($data_query as $item) {
            $temp = array(
                "id" => $item['doctor_id'],
                "name" => $item['doctor_name'],
                "title" => $item['doctor_title'],
                "images" => $item['doctor_images'],
                "position" => $item['doctor_location']
            );
            if (!in_array($temp, $doctor)) {
                $doctor[] = $temp;
            }
        }
        $this->view->faculty = $faculty;
        $this->view->hospital = $hospital;
        $this->view->doctor = $doctor;

        //hot_comment
        $model_rate = new Default_Model_Rate();
        $id_item = $model_rate->getPrefixFaculty() . '_' . $_id;
        $data_rate = $model_rate->getByItemIdTop1($id_item);

        
        $this->view->total_review = $data_query[0]['faculty_reviews'];
        $this->view->overall = $data_query[0]['faculty_overall'];
        $this->view->id_item = $id_item;

        $model_user = new Default_Model_User();
        $data_user = $model_user->getByuserId($data_rate[0]['id_user']);

        $this->view->displayname = $data_user['displayname'];
        $this->view->comment = $data_rate[0]['comment'];

        //rating
        $profileNameSpace = $this->view->profileNameSpace;
        if ($profileNameSpace) {
            $id_user = $profileNameSpace['id'];
            $data_user_rate = $model_rate->getByItemIdUserId($id_item, $id_user);
            if (!$data_user_rate)
                $this->view->isRating = false;
            else {
                $this->view->isRating = true;
                $this->view->rating = $data_user_rate;
            }
            $this->view->itemName = "processfaculty";
            $this->view->position = "position:relative;";
        }
    }

    public function ratingAction() {
        $profileNameSpace = $this->view->profileNameSpace;
        if ($profileNameSpace) {
            $type1 = $this->getParam('type1');
            $type2 = $this->getParam('type2');
            $type3 = $this->getParam('type3');
            $id_item = $this->getParam('id_item');
            $comment = $this->getParam('comment');
            $isRating = $this->getParam('isRating');
            $model_rate = new Default_Model_Rate();
            if ($isRating == 'false') {
                if ($model_rate->insert($id_item, $profileNameSpace['id'], $type1, $type2, $type3, $comment) != -1) {
                    $common = new Default_Model_Common();
                    $common->updateOverall($id_item, new Default_Model_Faculty());
                    $data = array('error' => '0');
                    $this->_helper->json->sendJson($data);
                } else {
                    $data = array('error' => '1');
                    $this->_helper->json->sendJson($data);
                }
            } else {
                $model_rate->setIdItem($id_item);
                $model_rate->setIdUser($profileNameSpace['id']);
                $model_rate->setType1($type1);
                $model_rate->setType2($type2);
                $model_rate->setType3($type3);
                $model_rate->setComment($comment);
                if ($model_rate->update() != -1) {
                    $common = new Default_Model_Common();
                    $common->updateOverall($id_item, new Default_Model_Faculty());
                    $data = array('error' => '0');
                    $this->_helper->json->sendJson($data);
                } else {
                    $data = array('error' => '1');
                    $this->_helper->json->sendJson($data);
                }
            }
        }
    }
}
